package linkedlist.leetcode_138;

public class Debug {
    public static Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        // 生成拷贝节点
        Node cur = head;
        while (cur != null) {
            Node temp = cur.next;
            Node copy = new Node(cur.val);
            cur.next = copy;
            copy.next = temp;
            cur = temp;
        }
        // 设置拷贝节点的 random
        cur = head;
        Node res = cur.next;
        Node copy = cur.next;
        while (cur != null) {
            Node temp = cur.next.next;
            copy.next = temp == null ? null : temp.next;
            copy.random = cur.random == null ? null : cur.random.next;
            copy = copy.next;
            cur.next = temp;
            cur = temp;
        }
        return res;
    }

    public static void main(String[] args) {
        Node one = new Node(7);
        Node two = new Node(13);
        Node three = new Node(11);
        Node four = new Node(10);
        Node five = new Node(1);
        one.next = two;
        two.next = three;
        three.next = four;
        four.next = five;

        one.random = null;
        two.random = one;
        three.random = five;
        four.random = three;
        five.random = one;
        copyRandomList(one);
    }
}
